using System;
using System.Text;

namespace Wilco.SyntaxHighlighting.Engine.Tokenizer
{
    /// <summary>
    /// Represents a character tokenizer.
    /// </summary>
    public class CharTokenizer : TokenizerBase
    {
        /// <summary>
        /// Initializes a new instance of a <see cref="CharTokenizer"/> class.
        /// </summary>
        public CharTokenizer()
        {
        }

        /// <summary>
        /// Tokenizes the source code.
        /// </summary>
        /// <param name="source">The source code to tokenize.</param>
        public override void Tokenize(string source)
        {
            Source = source;
        }

        /// <summary>
        /// Moves to the next token.
        /// </summary>
        /// <returns>Whether the iterator could be moved or not.</returns>
        public override bool MoveNext()
        {
            if ((Position + 1) >= Source.Length)
            {
                SetPosition(-1);
                return false;
            }
            else
            {
                SetPosition(Position + 1);
                return true;
            }
        }

        /// <summary>
        /// Gets the next token(s) without moving from position.
        /// </summary>
        /// <param name="count">The number of tokens which should be returned.</param>
        /// <returns>The tokens.</returns>
        public override string GetNextTokens(int count)
        {
            if (count <= 0)
                throw new ArgumentException("Can not be zero or less.", "count");
            if ((Position + count) > Source.Length)
                throw new ArgumentException("The sum of the current position and count is equal to or higher than the number of tokens.", "count");

            StringBuilder nextTokens = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                nextTokens.Append(Source[Position + i]);
            }
            return nextTokens.ToString();
        }
    }
}